﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKRevitBase.WinForm.HookSuit;
using YJKRevitBase.WinForm.WinFormRemindSuit;
using YJKRevitKernel.FormUtility;
using YJKCopyModel.Basic;
using YJKCopyModel.CommonTrans;
using YJKCopyModel.LastFastModeling;
using YJKCopyModel.Models;

namespace YJKCopyModel.Forms
{
	public partial class FastModelConvertForm : System.Windows.Forms.Form
	{
		public bool IsLeaveDlg
		{
			get
			{
				return this.m_bIsLeaveDlg;
			}
			set
			{
				this.m_bIsLeaveDlg = value;
			}
		}

		public string PublishGuid
		{
			get
			{
				return this.m_PublishGuid;
			}
			set
			{
				this.m_PublishGuid = value;
			}
		}

		public FastModelConvertForm()
		{
			this.InitializeComponent();
		}

		public FastModelConvertForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.m_Doc = doc;
		}

		private void FastModelConvertForm_Load(object sender, EventArgs e)
		{
			this.m_PublishGuid = Guid.NewGuid().ToString();
			this.m_hook = new MouseHook(this);
			this.m_hook.HookStart();
			//base.Icon = Resource.YArchitechIcon;
			this.InitData();
			List<KeyValuePair<ControlPacker, bool>> lastViewDatas = this.GetLastViewDatas();
			this.m_ctrlManager = new ControlInfoManager(this, lastViewDatas, "快模转化", null);
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.SetControlInfoToForm();
			}
		}

		private void FastModelConvertForm_KeyDown(object sender, KeyEventArgs e)
		{
			try
			{
				Keys keyCode = e.KeyCode;
				if (keyCode == Keys.Escape)
				{
					bool flag = !this.m_bIsLeaveDlg;
					if (flag)
					{
						base.Close();
					}
				}
			}
			catch
			{
			}
		}

		private void FastModelConvertForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			bool flag = this.m_hook != null;
			if (flag)
			{
				this.m_hook.HookStop();
			}
			int wMsg = 7;
			WindowsApi.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			WindowsApi.SendEsc();
			WindowsApi.SendEsc();
		}

		private void FastModelConvertForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			bool flag = this.m_ctrlManager != null;
			if (flag)
			{
				this.m_ctrlManager.GetControlInfoToXml();
			}
		}

		private void m_rbtnWholeLevel_CheckedChanged(object sender, EventArgs e)
		{
			this.m_PublishGuid = Guid.NewGuid().ToString();
			this.m_bDoHook = true;
			this.m_ViewParas.IsWholeLevel = this.m_rbtnWholeLevel.Checked;
		}

		private void m_cmbFamilyCategorys_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_bDoHook = true;
			this.m_cmbFamilyNames.Items.Clear();
			this.m_cmbFamilySymbols.Items.Clear();
			string text = this.m_cmbFamilyCategorys.Text;
			bool flag = string.IsNullOrEmpty(text);
			if (!flag)
			{
				List<Family> familysFromDic = this.GetFamilysFromDic(this.m_ExistFamilys, text);
				List<string> familyNames = this.GetFamilyNames(familysFromDic);
				BussinessCommon.LoadComboBoxDatas(this.m_cmbFamilyNames, familyNames);
				bool flag2 = this.m_cmbFamilyNames.Items != null && this.m_cmbFamilyNames.Items.Count > 0;
				if (flag2)
				{
					this.m_cmbFamilyNames.SelectedIndex = 0;
				}
			}
		}

		private void m_cmbFamilyNames_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_bDoHook = true;
			this.m_cmbFamilySymbols.Items.Clear();
			string text = this.m_cmbFamilyNames.Text;
			bool flag = string.IsNullOrEmpty(text);
			if (!flag)
			{
				Family familyFromDoc = RevitCommonFunctions.GetFamilyFromDoc(this.m_Doc, text);
				List<string> familySymbolNames = RevitCommonFunctions.GetFamilySymbolNames(familyFromDoc);
				BussinessCommon.LoadComboBoxDatas(this.m_cmbFamilySymbols, familySymbolNames);
				bool flag2 = this.m_cmbFamilySymbols.Items != null && this.m_cmbFamilySymbols.Items.Count > 0;
				if (flag2)
				{
					this.m_cmbFamilySymbols.SelectedIndex = 0;
				}
			}
		}

		private void m_cmbFamilySymbols_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.m_bDoHook = true;
		}

		private void m_nudLevel_ValueChanged(object sender, EventArgs e)
		{
		}

		private void m_rbtn_CheckedChanged(object sender, EventArgs e)
		{
			this.m_bDoHook = true;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg != 1125)
			{
				base.DefWndProc(ref m);
			}
			else
			{
				bool flag = this.m_bDoHook && !this.m_bIsLeaveDlg;
				if (flag)
				{
					this.MouseLeaveForm();
				}
			}
		}

		private void MouseLeaveForm()
		{
			bool flag = !base.ClientRectangle.Contains(base.PointToClient(Cursor.Position));
			if (flag)
			{
				bool flag2 = !this.Checking();
				if (flag2)
				{
					WindowsApi.SetCursorPos(base.Location.X + base.Width / 2, base.Location.Y + base.Height / 2);
					WindowsApi.SetActiveWindow(base.Handle);
					base.Activate();
					base.Focus();
				}
				else
				{
					int wMsg = 7;
					WindowsApi.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					WindowsApi.SendEsc();
					this.m_bIsLeaveDlg = true;
					this.m_bDoHook = false;
				}
			}
			else
			{
				WindowsApi.SetActiveWindow(base.Handle);
				base.Activate();
				base.Focus();
			}
		}

		private void InitData()
		{
			List<string> blockConvertCategoryNames = SpecialtyXml.GetBlockConvertCategoryNames();
			this.m_ExistFamilys = this.InitFamilysByCategorys(blockConvertCategoryNames);
			bool flag = this.m_ExistFamilys == null || this.m_ExistFamilys.Count <= 0;
			if (!flag)
			{
				List<string> datas = this.m_ExistFamilys.Keys.ToList<string>();
				BussinessCommon.LoadComboBoxDatas(this.m_cmbFamilyCategorys, datas);
				bool flag2 = this.m_cmbFamilyCategorys.Items != null && this.m_cmbFamilyCategorys.Items.Count > 0;
				if (flag2)
				{
					this.m_cmbFamilyCategorys.SelectedIndex = 0;
				}
				else
				{
					YJKMessageBox.Show("当前项目不存在基于点布置的族！");
				}
			}
		}

		private Dictionary<string, List<Family>> InitFamilysByCategorys(List<string> categoryNames)
		{
			bool flag = categoryNames.IsEmpty<string>();
			Dictionary<string, List<Family>> result;
			if (flag)
			{
				result = new Dictionary<string, List<Family>>();
			}
			else
			{
				RevitCategoryHelper revitCategoryHelper = new RevitCategoryHelper();
				Dictionary<string, List<Family>> dictionary = new Dictionary<string, List<Family>>();
				foreach (string text in categoryNames)
				{
					List<BuiltInCategory> categorysFromDic = revitCategoryHelper.GetCategorysFromDic(BussinessCommon.GetListFromString(text));
					List<string> containInfos = null;
					List<string> noContainInfos = null;
					List<Family> curDocFamilys = RevitCommonFunctions.GetCurDocFamilys(this.m_Doc, categorysFromDic, containInfos, noContainInfos);
					List<Family> basePointPlaceFamilys = RevitCommonFunctions.GetBasePointPlaceFamilys(curDocFamilys);
					bool flag2 = basePointPlaceFamilys.IsEmpty<Family>();
					if (!flag2)
					{
						bool flag3 = !dictionary.ContainsKey(text);
						if (flag3)
						{
							dictionary.Add(text, basePointPlaceFamilys);
						}
					}
				}
				result = dictionary;
			}
			return result;
		}

		private List<Family> GetFamilysFromDic(Dictionary<string, List<Family>> dic, string key)
		{
			bool flag = dic == null || dic.Count <= 0;
			List<Family> result;
			if (flag)
			{
				result = new List<Family>();
			}
			else
			{
				bool flag2 = dic.ContainsKey(key);
				if (flag2)
				{
					result = dic[key];
				}
				else
				{
					result = new List<Family>();
				}
			}
			return result;
		}

		private bool Checking()
		{
			string message = string.Empty;
			bool flag = string.IsNullOrEmpty(this.m_cmbFamilySymbols.Text);
			bool result;
			if (flag)
			{
				message = "族类型不能为空！";
				YJKMessageBox.Show(message);
				result = false;
			}
			else
			{
				bool flag2 = string.IsNullOrEmpty(this.m_nudLevel.Text);
				if (flag2)
				{
					message = "相对标高不能为空！";
					YJKMessageBox.Show(message);
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}

		private List<string> GetFamilyNames(List<Family> familys)
		{
			bool flag = familys == null || familys.Count <= 0;
			List<string> result;
			if (flag)
			{
				result = new List<string>();
			}
			else
			{
				List<string> list = new List<string>();
				foreach (Family family in familys)
				{
					bool flag2 = family == null;
					if (!flag2)
					{
						string name = family.Name;
						bool flag3 = string.IsNullOrEmpty(name) || string.IsNullOrEmpty(name.Trim()) || list.Contains(name.Trim());
						if (!flag3)
						{
							list.Add(name.Trim());
						}
					}
				}
				list.Sort();
				result = list;
			}
			return result;
		}

		public List<KeyValuePair<ControlPacker, bool>> GetLastViewDatas()
		{
			List<KeyValuePair<ControlPacker, bool>> list = new List<KeyValuePair<ControlPacker, bool>>();
			bool flag = this.panel3.Controls.Count <= 0;
			List<KeyValuePair<ControlPacker, bool>> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnWholeLevel,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnArea,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnCenterToCenter,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnCenterToBase,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_rbtnBaseToBase,
					UseCustomFormatter = new RadioButtonControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_cmbFamilyCategorys,
					UseCustomFormatter = new ComboBoxControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_cmbFamilyNames,
					UseCustomFormatter = new ComboBoxControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_cmbFamilySymbols,
					UseCustomFormatter = new ComboBoxControlInfoTransform()
				}, true));
				list.Add(new KeyValuePair<ControlPacker, bool>(new ControlPacker
				{
					UseControl = this.m_nudLevel,
					UseCustomFormatter = new NumericUpDownControlInfoTransform()
				}, true));
				result = list;
			}
			return result;
		}

		public FastModelConvertPara GetViewParas()
		{
			bool flag = this.m_ViewParas == null;
			if (flag)
			{
				this.m_ViewParas = new FastModelConvertPara();
			}
			this.m_ViewParas.SelectedFamilyName = this.m_cmbFamilyNames.Text.Trim();
			this.m_ViewParas.SelectedModel = this.m_cmbFamilySymbols.Text.Trim();
			this.m_ViewParas.LocationType = this.GetLocationType();
			this.m_ViewParas.Offset = BussinessCommon.GetDoubleByText(this.m_nudLevel.Text, 3);
			this.m_ViewParas.IsWholeLevel = this.m_rbtnWholeLevel.Checked;
			return this.m_ViewParas;
		}

		public int GetLocationType()
		{
			bool @checked = this.m_rbtnCenterToCenter.Checked;
			int result;
			if (@checked)
			{
				result = 0;
			}
			else
			{
				bool checked2 = this.m_rbtnCenterToBase.Checked;
				if (checked2)
				{
					result = 1;
				}
				else
				{
					bool checked3 = this.m_rbtnBaseToBase.Checked;
					if (checked3)
					{
						result = 2;
					}
					else
					{
						result = 0;
					}
				}
			}
			return result;
		}

		private ControlInfoManager m_ctrlManager = null;

		private MouseHook m_hook = null;

		public bool m_bDoHook = false;

		private Document m_Doc;

		private FastModelConvertPara m_ViewParas = new FastModelConvertPara();

		private Dictionary<string, List<Family>> m_ExistFamilys = null;

		private bool m_bIsLeaveDlg = false;

		private string m_PublishGuid;
	}
}
